# -*- coding:utf-8 -*-
from django.contrib import admin
from django import forms
from mptt.admin import MPTTModelAdmin

from jpkc.models import DownloadItem, Article, Column, Question, Answer

class ColumnAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ColumnAdminForm, self).__init__(*args, **kwargs)
        self.fields['parent'].queryset = Column.objects.exclude(id=self.instance.id)


    # parent = forms.ModelChoiceField(queryset=Column.objects.exclude(id=self.instance.id))
    class Meta:
        model = Column


class ColumnAdmin(MPTTModelAdmin):
    form = ColumnAdminForm

    list_display = ['title', 'article_count', 'is_root_column', ]


    class Media:
        js = [
            #'/media/admin/tinymce/jscripts/tiny_mce/tiny_mce.js',
            '/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js',
            '/static/grappelli/tinymce_setup/tinymce_setup.js',
            # '/static/filebrowser/js/tinymceadmin.js',
        ]


class DownloadItemInLine(admin.StackedInline):
    model = DownloadItem
    extra = 1


class ArticleForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        self.fields['column'].queryset = Column.objects.exclude(parent=None)

    class Meta:
        model = Article


class ArticleAdmin(admin.ModelAdmin):
    list_display = ['title', 'column', 'pub_date', ]
    inlines = [DownloadItemInLine, ]
    form = ArticleForm


    class Media:
        js = [
            #'/media/admin/tinymce/jscripts/tiny_mce/tiny_mce.js',
            '/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js',
            '/static/grappelli/tinymce_setup/tinymce_setup.js',
            # '/static/filebrowser/js/tinymceadmin.js',
        ]


class AnswerForm(forms.ModelForm):
    class Meta:
        model = Answer
        exclude = ['solver', ]


class AnswerAdmin(admin.ModelAdmin):
    raw_id_fields = ('question', )
    model = Answer
    #exclude = ['solver']
    form = AnswerForm

    def save_model(self, request, obj, form, change):
        if not change:
            obj.solver = request.user
        obj.save()

    def queryset(self, request):
        if not request.user.is_superuser:
            return Answer.objects.filter(solver=request.user)
        return  Answer.objects.all()

    def has_change_permission(self, request, obj = None):
        if not obj or request.user.is_superuser:
            return True
        else:
            return obj.solver == request.user

    def has_delete_permission(self, request, obj = None):
        return self.has_change_permission(request, obj)


class AnswerInLine(admin.StackedInline):
    model = Answer
    form = AnswerForm
    extra = 1


class QuestionAdmin(admin.ModelAdmin):
    model = Question
    readonly_fields = ['asker', 'content', 'status', ]
    inlines = [AnswerInLine, ]

    list_display = ['asker', 'pub_date', 'status', ]

    def has_add_permission(self, request):
        return False


    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)

        for instance in instances:
            if isinstance(instance, Answer):
                instance.solver = request.user
                instance.save()
        formset.save_m2m()


class DownloadItemAdmin(admin.ModelAdmin):
    list_display = ['title', 'used_by_article', ]


admin.site.register(Column, ColumnAdmin)
admin.site.register(Article, ArticleAdmin)
admin.site.register(DownloadItem, DownloadItemAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(Answer, AnswerAdmin)